<template>
  <div>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>{{ element?.options?.header }}</span>
          <el-button class="button" text>操作按钮</el-button>
        </div>
      </template>
      <div class="draggable-container card-body">
        <draggable :key="element.metaRefresh" class="drawing-board" v-model="element.items" @add="handleAdd($event)"
          v-bind="{ group: 'group', animation: 200 }" tag="div" item-key="key" handle=".mover"
          :component-data="{ name: 'fade' }" :disabled="lowCode.disabled && element?.nestedChain?.indexOf('dialog') == -1
            ">
          <template #item="{ element: el, index }">
            <div>
              <low-code-parse :element="el" :key="el.key" :index="index" :parent="element">
              </low-code-parse>
            </div>
          </template>
        </draggable>
      </div>
    </el-card>
  </div>
</template>
 
<script lang="ts">
import { defineComponent, inject, toRefs } from "vue";
import type { LowCodeImp, ComponentImp } from "@/components/low-code/core/type";
import { tools } from "@/components/low-code/page-designer/utils";
import clone from "clone";
import { useComponentPretreatment } from "@/components/low-code/page-designer/hooks";
import { ElMessage } from "element-plus";

export default defineComponent({
  name: "cardWidget",
  props: ["element"],
  setup(props) {
    const getLowCodeInstance = inject("getLowCodeInstance") as Function;
    const lowCode = getLowCodeInstance() as LowCodeImp;
    const { element } = toRefs(props);

    const handleAdd = (event: { newIndex: number }) => {
      let hasKey = false;
      const newIndex = event.newIndex;
      if (element.value.items[newIndex]) {
        /****
         * 组件预处理
         */
        if (useComponentPretreatment(lowCode, element.value, newIndex)) return false;

        hasKey = element.value.items[newIndex].key ? true : false;
        let cacheObject = lowCode.addComponent(
          element.value.items[newIndex]
        ) as ComponentImp;
        cacheObject.nestedChain = element.value.nestedChain + "_card";
        element.value.items[newIndex] = cacheObject;
        lowCode.setcurrentKey(cacheObject.key);
      }
      console.log("element.value.key", element.value.key);
      hasKey && element.value.updateMetaRefresh();
      tools.handleStack.push(clone(lowCode));
    };

    return {
      lowCode,
      element,
      handleAdd,
    };
  },
});
</script>

<style  scoped>
.drawing-board {
  min-height: 150px;
}
</style>
